home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / genoutput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  27.3 KB  |  1,028 lines  |  [TEXT/MPS ]

  1. /* Generate code from to output assembler insns as recognized from rtl.
  2.    Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This program reads the machine description for the compiler target machine
  22.    and produces a file containing these things:
  23.  
  24.    1. An array of strings `insn_template' which is indexed by insn code number
  25.    and contains the template for output of that insn,
  26.  
  27.    2. An array of functions `insn_outfun' which, indexed by the insn code
  28.    number, gives the function that returns a template to use for output of
  29.    that insn.  This is used only in the cases where the template is not
  30.    constant.  These cases are specified by a * or @ at the beginning of the
  31.    template string in the machine description.  They are identified for the
  32.    sake of other parts of the compiler by a zero element in `insn_template'.
  33.   
  34.    3. An array of functions `insn_gen_function' which, indexed
  35.    by insn code number, gives the function to generate a body
  36.    for that pattern, given operands as arguments.
  37.  
  38.    4. An array of strings `insn_name' which, indexed by insn code number,
  39.    gives the name for that pattern.  Nameless patterns are given a name.
  40.  
  41.    5. An array of ints `insn_n_operands' which is indexed by insn code number
  42.    and contains the number of distinct operands in the pattern for that insn,
  43.  
  44.    6. An array of ints `insn_n_dups' which is indexed by insn code number
  45.    and contains the number of match_dup's that appear in the insn's pattern.
  46.    This says how many elements of `recog_dup_loc' are significant
  47.    after an insn has been recognized.
  48.  
  49.    7. An array of arrays of operand constraint strings,
  50.    `insn_operand_constraint',
  51.    indexed first by insn code number and second by operand number,
  52.    containing the constraint for that operand.
  53.  
  54.    This array is generated only if register constraints appear in 
  55.    match_operand rtx's.
  56.  
  57.    8. An array of arrays of chars which indicate which operands of
  58.    which insn patterns appear within ADDRESS rtx's.  This array is
  59.    called `insn_operand_address_p' and is generated only if there
  60.    are *no* register constraints in the match_operand rtx's.
  61.  
  62.    9. An array of arrays of machine modes, `insn_operand_mode',
  63.    indexed first by insn code number and second by operand number,
  64.    containing the machine mode that that operand is supposed to have.
  65.    Also `insn_operand_strict_low', which is nonzero for operands
  66.    contained in a STRICT_LOW_PART.
  67.  
  68.    10. An array of arrays of int-valued functions, `insn_operand_predicate',
  69.    indexed first by insn code number and second by operand number,
  70.    containing the match_operand predicate for this operand.
  71.  
  72.    11. An array of ints, `insn_n_alternatives', that gives the number
  73.    of alternatives in the constraints of each pattern.
  74.  
  75. The code number of an insn is simply its position in the machine description;
  76. code numbers are assigned sequentially to entries in the description,
  77. starting with code number 0.
  78.  
  79. Thus, the following entry in the machine description
  80.  
  81.     (define_insn "clrdf"
  82.       [(set (match_operand:DF 0 "general_operand" "")
  83.         (const_int 0))]
  84.       ""
  85.       "clrd %0")
  86.  
  87. assuming it is the 25th entry present, would cause
  88. insn_template[24] to be "clrd %0", and insn_n_operands[24] to be 1.
  89. It would not make an case in output_insn_hairy because the template
  90. given in the entry is a constant (it does not start with `*').  */
  91.  
  92. #include <stdio.h>
  93. #include "hconfig.h"
  94. #include "rtl.h"
  95. #include "obstack.h"
  96.  
  97. /* No instruction can have more operands than this.
  98.    Sorry for this arbitrary limit, but what machine will
  99.    have an instruction with this many operands?  */
  100.  
  101. #define MAX_MAX_OPERANDS 40
  102.  
  103. static struct obstack obstack;
  104. struct obstack *rtl_obstack = &obstack;
  105.  
  106. #define obstack_chunk_alloc xmalloc
  107. #define obstack_chunk_free free
  108.  
  109. extern void free ();
  110. extern rtx read_rtx ();
  111. #ifdef APPLE_HAX
  112. FILE *infile;
  113. #endif
  114.  
  115. char *xmalloc ();
  116. static void fatal ();
  117. void fancy_abort ();
  118. static void error ();
  119. static void mybcopy ();
  120. static void mybzero ();
  121. static int n_occurrences ();
  122.  
  123. /* insns in the machine description are assigned sequential code numbers
  124.    that are used by insn-recog.c (produced by genrecog) to communicate
  125.    to insn-output.c (produced by this program).  */
  126.  
  127. static int next_code_number;
  128.  
  129. /* This counts all definitions in the md file,
  130.    for the sake of error messages.  */
  131.  
  132. static int next_index_number;
  133.  
  134. /* Record in this chain all information that we will output,
  135.    associated with the code number of the insn.  */
  136.  
  137. struct data
  138. {
  139.   int code_number;
  140.   int index_number;
  141.   char *name;
  142.   char *template;        /* string such as "movl %1,%0" */
  143.   int n_operands;        /* Number of operands this insn recognizes */
  144.   int n_dups;            /* Number times match_dup appears in pattern */
  145.   int n_alternatives;        /* Number of alternatives in each constraint */
  146.   struct data *next;
  147.   char *constraints[MAX_MAX_OPERANDS];
  148.   /* Number of alternatives in constraints of operand N.  */
  149.   int op_n_alternatives[MAX_MAX_OPERANDS];
  150.   char *predicates[MAX_MAX_OPERANDS];
  151.   char address_p[MAX_MAX_OPERANDS];
  152.   enum machine_mode modes[MAX_MAX_OPERANDS];
  153.   char strict_low[MAX_MAX_OPERANDS];
  154.   char outfun;            /* Nonzero means this has an output function */
  155. };
  156.  
  157. /* This variable points to the first link in the chain.  */
  158.  
  159. struct data *insn_data;
  160.  
  161. /* Pointer to the last link in the chain, so new elements
  162.    can be added at the end.  */
  163.  
  164. struct data *end_of_insn_data;
  165.  
  166. /* Nonzero if any match_operand has a constraint string;
  167.    implies that REGISTER_CONSTRAINTS will be defined
  168.    for this machine description.  */
  169.  
  170. int have_constraints;
  171.  
  172. static void
  173. output_prologue ()
  174. {
  175.  
  176.   printf ("/* Generated automatically by the program `genoutput'\n\
  177. from the machine description file `md'.  */\n\n");
  178.  
  179.   printf ("#include \"config.h\"\n");
  180.   printf ("#include \"rtl.h\"\n");
  181.   printf ("#include \"regs.h\"\n");
  182.   printf ("#include \"hard-reg-set.h\"\n");
  183.   printf ("#include \"real.h\"\n");
  184.   printf ("#include \"insn-config.h\"\n\n");
  185.   printf ("#include \"conditions.h\"\n");
  186.   printf ("#include \"insn-flags.h\"\n");
  187.   printf ("#include \"insn-attr.h\"\n\n");
  188.   printf ("#include \"insn-codes.h\"\n\n");
  189.   printf ("#include \"recog.h\"\n\n");
  190.  
  191.   printf ("#include <stdio.h>\n");
  192.   printf ("#include \"output.h\"\n");
  193. }
  194.  
  195. static void
  196. output_epilogue ()
  197. {
  198.   register struct data *d;
  199.  
  200.   printf ("\nchar * const insn_template[] =\n  {\n");
  201.   for (d = insn_data; d; d = d->next)
  202.     {
  203.       if (d->template)
  204.     printf ("    \"%s\",\n", d->template);
  205.       else
  206.     printf ("    0,\n");
  207.     }
  208.   printf ("  };\n");
  209.  
  210.   printf ("\nchar *(*const insn_outfun[])() =\n  {\n");
  211.   for (d = insn_data; d; d = d->next)
  212.     {
  213.       if (d->outfun)
  214.     printf ("    output_%d,\n", d->code_number);
  215.       else
  216.     printf ("    0,\n");
  217.     }
  218.   printf ("  };\n");
  219.  
  220.   printf ("\nrtx (*const insn_gen_function[]) () =\n  {\n");
  221.   for (d = insn_data; d; d = d->next)
  222.     {
  223.       if (d->name)
  224.     printf ("    gen_%s,\n", d->name);
  225.       else
  226.     printf ("    0,\n");
  227.     }
  228.   printf ("  };\n");
  229.  
  230.   printf ("\nchar *insn_name[] =\n  {\n");
  231.   {
  232.     int offset = 0;
  233.     int next;
  234.     char * last_name = 0;
  235.     char * next_name;
  236.     register struct data *n;
  237.  
  238.     for (n = insn_data, next = 0; n; n = n->next, next++)
  239.       if (n->name)
  240.     {
  241.       next_name = n->name;
  242.       break;
  243.     }
  244.  
  245.     for (d = insn_data; d; d = d->next)
  246.       {
  247.     if (d->name)
  248.       {
  249.         printf ("    \"%s\",\n", d->name);
  250.         offset = 0;
  251.         last_name = d->name;
  252.         next_name = 0;
  253.         for (n = d->next, next = 1; n; n = n->next, next++)
  254.           if (n->name)
  255.         {
  256.           next_name = n->name;
  257.           break;
  258.         }
  259.       }
  260.     else
  261.       {
  262.         offset++;
  263.         if (next_name && (last_name == 0 || offset > next / 2))
  264.           printf ("    \"%s-%d\",\n", next_name, next - offset);
  265.         else
  266.           printf ("    \"%s+%d\",\n", last_name, offset);
  267.       }
  268.       }
  269.   }
  270.   printf ("  };\n");
  271.   printf ("char **insn_name_ptr = insn_name;\n");
  272.  
  273.   printf ("\nconst int insn_n_operands[] =\n  {\n");
  274.   for (d = insn_data; d; d = d->next)
  275.     printf ("    %d,\n", d->n_operands);
  276.   printf ("  };\n");
  277.  
  278.   printf ("\nconst int insn_n_dups[] =\n  {\n");
  279.   for (d = insn_data; d; d = d->next)
  280.     printf ("    %d,\n", d->n_dups);
  281.   printf ("  };\n");
  282.  
  283.   if (have_constraints)
  284.     {
  285.       printf ("\nchar *const insn_operand_constraint[][MAX_RECOG_OPERANDS] =\n  {\n");
  286.       for (d = insn_data; d; d = d->next)
  287.     {
  288.       register int i;
  289.       printf ("    {");
  290.       for (i = 0; i < d->n_operands; i++)
  291.         {
  292.           if (d->constraints[i] == 0)
  293.         printf (" \"\",");
  294.           else
  295.         printf (" \"%s\",", d->constraints[i]);
  296.         }
  297.       if (d->n_operands == 0)
  298.         printf (" 0");
  299.       printf (" },\n");
  300.     }
  301.       printf ("  };\n");
  302.     }
  303.   else
  304.     {
  305.       printf ("\nconst char insn_operand_address_p[][MAX_RECOG_OPERANDS] =\n  {\n");
  306.       for (d = insn_data; d; d = d->next)
  307.     {
  308.       register int i;
  309.       printf ("    {");
  310.       for (i = 0; i < d->n_operands; i++)
  311.         printf (" %d,", d->address_p[i]);
  312.       if (d->n_operands == 0)
  313.         printf (" 0");
  314.       printf (" },\n");
  315.     }
  316.       printf ("  };\n");
  317.     }
  318.  
  319.   printf ("\nconst enum machine_mode insn_operand_mode[][MAX_RECOG_OPERANDS] =\n  {\n");
  320.   for (d = insn_data; d; d = d->next)
  321.     {
  322.       register int i;
  323. #ifdef MPW_C
  324.       register char *temp;
  325. #endif      
  326.       printf ("    {");
  327. #ifdef MPW_C
  328.       i = 0;
  329.       while (i < d->n_operands) {
  330.           temp = GET_MODE_NAME (d->modes[i]);
  331.           printf (" %s", temp); 
  332.     printf ("mode,");
  333.     i++;
  334.       }
  335. #else
  336.       for (i = 0; i < d->n_operands; i++)
  337.     printf (" %smode,", GET_MODE_NAME (d->modes[i]));
  338. #endif
  339.       if (d->n_operands == 0)
  340.     printf (" VOIDmode");
  341.       printf (" },\n");
  342.     }
  343.   printf ("  };\n");
  344.  
  345.   printf ("\nconst char insn_operand_strict_low[][MAX_RECOG_OPERANDS] =\n  {\n");
  346.   for (d = insn_data; d; d = d->next)
  347.     {
  348.       register int i;
  349.       printf ("    {");
  350.       for (i = 0; i < d->n_operands; i++)
  351.     printf (" %d,", d->strict_low[i]);
  352.       if (d->n_operands == 0)
  353.     printf (" 0");
  354.       printf (" },\n");
  355.     }
  356.   printf ("  };\n");
  357.  
  358.   {
  359.     /* We need to define all predicates used.  Keep a list of those we
  360.        have defined so far.  There normally aren't very many predicates used,
  361.        so a linked list should be fast enough.  */
  362.     struct predicate { char *name; struct predicate *next; } *predicates = 0;
  363.     struct predicate *p;
  364.     int i;
  365.  
  366.     printf ("\n");
  367.     for (d = insn_data; d; d = d->next)
  368.       for (i = 0; i < d->n_operands; i++)
  369.     if (d->predicates[i] && d->predicates[i][0])
  370.       {
  371.         for (p = predicates; p; p = p->next)
  372.           if (! strcmp (p->name, d->predicates[i]))
  373.         break;
  374.  
  375.         if (p == 0)
  376.           {
  377.         printf ("extern int %s ();\n", d->predicates[i]);
  378.         p = (struct predicate *) alloca (sizeof (struct predicate));
  379.         p->name = d->predicates[i];
  380.         p->next = predicates;
  381.         predicates = p;
  382.           }
  383.       }
  384.     
  385.     printf ("\nint (*const insn_operand_predicate[][MAX_RECOG_OPERANDS])() =\n  {\n");
  386.     for (d = insn_data; d; d = d->next)
  387.       {
  388.     printf ("    {");
  389.     for (i = 0; i < d->n_operands; i++)
  390.       printf (" %s,", ((d->predicates[i] && d->predicates[i][0])
  391.                ? d->predicates[i] : "0"));
  392.     if (d->n_operands == 0)
  393.       printf (" 0");
  394.     printf (" },\n");
  395.       }
  396.     printf ("  };\n");
  397.   }
  398.  
  399.   printf ("\nconst int insn_n_alternatives[] =\n  {\n");
  400.   for (d = insn_data; d; d = d->next)
  401.     printf ("    %d,\n", d->n_alternatives);
  402.   printf("  };\n");
  403. }
  404.  
  405. /* scan_operands (X) stores in max_opno the largest operand
  406.    number present in X, if that is larger than the previous
  407.    value of max_opno.  It stores all the constraints in `constraints'
  408.    and all the machine modes in `modes'.
  409.  
  410.    THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
  411.    THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART.  */
  412.  
  413. static int max_opno;
  414. static int num_dups;
  415. static char *constraints[MAX_MAX_OPERANDS];
  416. static int op_n_alternatives[MAX_MAX_OPERANDS];
  417. static char *predicates[MAX_MAX_OPERANDS];
  418. static char address_p[MAX_MAX_OPERANDS];
  419. static enum machine_mode modes[MAX_MAX_OPERANDS];
  420. static char strict_low[MAX_MAX_OPERANDS];
  421. static char seen[MAX_MAX_OPERANDS];
  422.  
  423. static void
  424. scan_operands (part, this_address_p, this_strict_low)
  425.      rtx part;
  426.      int this_address_p;
  427.      int this_strict_low;
  428. {
  429.   register int i, j;
  430.   register char *format_ptr;
  431.   int opno;
  432.  
  433.   if (part == 0)
  434.     return;
  435.  
  436.   switch (GET_CODE (part))
  437.     {
  438.     case MATCH_OPERAND:
  439.       opno = XINT (part, 0);
  440.       if (opno > max_opno)
  441.     max_opno = opno;
  442.       if (max_opno >= MAX_MAX_OPERANDS)
  443.     {
  444.       error ("Too many operands (%d) in definition %d.\n",
  445.          max_opno + 1, next_index_number);
  446.       return;
  447.     }
  448.       if (seen[opno])
  449.     error ("Definition %d specified operand number %d more than once.\n",
  450.            next_index_number, opno);
  451.       seen[opno] = 1;
  452.       modes[opno] = GET_MODE (part);
  453.       strict_low[opno] = this_strict_low;
  454.       predicates[opno] = XSTR (part, 1);
  455.       constraints[opno] = XSTR (part, 2);
  456.       if (XSTR (part, 2) != 0 && *XSTR (part, 2) != 0)
  457.     {
  458.       op_n_alternatives[opno] = n_occurrences (',', XSTR (part, 2)) + 1;
  459.       have_constraints = 1;
  460.     }
  461.       address_p[opno] = this_address_p;
  462.       return;
  463.  
  464.     case MATCH_SCRATCH:
  465.       opno = XINT (part, 0);
  466.       if (opno > max_opno)
  467.     max_opno = opno;
  468.       if (max_opno >= MAX_MAX_OPERANDS)
  469.     {
  470.       error ("Too many operands (%d) in definition %d.\n",
  471.          max_opno + 1, next_index_number);
  472.       return;
  473.     }
  474.       if (seen[opno])
  475.     error ("Definition %d specified operand number %d more than once.\n",
  476.            next_index_number, opno);
  477.       seen[opno] = 1;
  478.       modes[opno] = GET_MODE (part);
  479.       strict_low[opno] = 0;
  480.       predicates[opno] = "scratch_operand";
  481.       constraints[opno] = XSTR (part, 1);
  482.       if (XSTR (part, 1) != 0 && *XSTR (part, 1) != 0)
  483.     {
  484.       op_n_alternatives[opno] = n_occurrences (',', XSTR (part, 1)) + 1;
  485.       have_constraints = 1;
  486.     }
  487.       address_p[opno] = 0;
  488.       return;
  489.  
  490.     case MATCH_OPERATOR:
  491.     case MATCH_PARALLEL:
  492.       opno = XINT (part, 0);
  493.       if (opno > max_opno)
  494.     max_opno = opno;
  495.       if (max_opno >= MAX_MAX_OPERANDS)
  496.     {
  497.       error ("Too many operands (%d) in definition %d.\n",
  498.          max_opno + 1, next_index_number);
  499.       return;
  500.     }
  501.       if (seen[opno])
  502.     error ("Definition %d specified operand number %d more than once.\n",
  503.            next_index_number, opno);
  504.       seen[opno] = 1;
  505.       modes[opno] = GET_MODE (part);
  506.       strict_low[opno] = 0;
  507.       predicates[opno] = XSTR (part, 1);
  508.       constraints[opno] = 0;
  509.       address_p[opno] = 0;
  510.       for (i = 0; i < XVECLEN (part, 2); i++)
  511.     scan_operands (XVECEXP (part, 2, i), 0, 0);
  512.       return;
  513.  
  514.     case MATCH_DUP:
  515.     case MATCH_OP_DUP:
  516.     case MATCH_PAR_DUP:
  517.       ++num_dups;
  518.       return;
  519.  
  520.     case ADDRESS:
  521.       scan_operands (XEXP (part, 0), 1, 0);
  522.       return;
  523.  
  524.     case STRICT_LOW_PART:
  525.       scan_operands (XEXP (part, 0), 0, 1);
  526.       return;
  527.     }
  528.  
  529.   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
  530.  
  531.   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
  532.     switch (*format_ptr++)
  533.       {
  534.       case 'e':
  535.     scan_operands (XEXP (part, i), 0, 0);
  536.     break;
  537.       case 'E':
  538.     if (XVEC (part, i) != NULL)
  539.       for (j = 0; j < XVECLEN (part, i); j++)
  540.         scan_operands (XVECEXP (part, i, j), 0, 0);
  541.     break;
  542.       }
  543. }
  544.  
  545. /* Process an assembler template from a define_insn or a define_peephole.
  546.    It is either the assembler code template, a list of assembler code
  547.    templates, or C code to generate the assembler code template.  */
  548.  
  549. static void
  550. process_template (d, template)
  551.     struct data *d;
  552.     char *template;
  553. {
  554.   register char *cp;
  555.   register int i;
  556.  
  557.   /* We need to consider only the instructions whose assembler code template
  558.      starts with a * or @.  These are the ones where C code is run to decide
  559.      on a template to use.  So for all others just return now.  */
  560.  
  561.   if (template[0] != '*' && template[0] != '@')
  562.     {
  563.       d->template = template;
  564.       d->outfun = 0;
  565.       return;
  566.     }
  567.  
  568.   d->template = 0;
  569.   d->outfun = 1;
  570. #ifdef MPW
  571.   /* Since the insn-output file may be too large to fit in a segment, this bit
  572.      of code issues segment pragmas periodically. */
  573.   {
  574.     static mpw_hack = 0;
  575.     if (mpw_hack++ == 25)
  576.       {
  577.         printf("\n#ifdef MPW\n#pragma segment IO%d\n#endif\n", mpw_hack);
  578.       }
  579.   }
  580. #endif
  581.   printf ("\nstatic char *\n");
  582.   printf ("output_%d (operands, insn)\n", d->code_number);
  583.   printf ("     rtx *operands;\n");
  584.   printf ("     rtx insn;\n");
  585.   printf ("{\n");
  586.  
  587.   /* If the assembler code template starts with a @ it is a newline-separated
  588.      list of assembler code templates, one for each alternative.  So produce
  589.      a routine to select the correct one.  */
  590.  
  591.   if (template[0] == '@')
  592.     {
  593.  
  594.       printf ("  static /*const*/ char *const strings_%d[] = {\n",
  595.           d->code_number);
  596.  
  597.       for (i = 0, cp = &template[1]; *cp; )
  598.     {
  599.       while (*cp == '\n' || *cp == ' ' || *cp== '\t')
  600.         cp++;
  601.  
  602.       printf ("    \"");
  603.       while (*cp != '\n' && *cp != '\0')
  604.         putchar (*cp++);
  605.  
  606.       printf ("\",\n");
  607.       i++;
  608.     }
  609.  
  610.       printf ("  };\n");
  611.       printf ("  return strings_%d[which_alternative];\n", d->code_number);
  612.  
  613.       if (i != d->n_alternatives)
  614.     fatal ("Insn pattern %d has %d alternatives but %d assembler choices",
  615.            d->index_number, d->n_alternatives, i);
  616.  
  617.     }
  618.   else
  619.     {
  620.        /* The following is done in a funny way to get around problems in
  621.       VAX-11 "C" on VMS.  It is the equivalent of:
  622.         printf ("%s\n", &template[1])); */
  623.       cp = &template[1];
  624.       while (*cp) putchar (*cp++);
  625.       putchar ('\n');
  626.     }
  627.  
  628.   printf ("}\n");
  629. }
  630.  
  631. /* Check insn D for consistency in number of constraint alternatives.  */
  632.  
  633. static void
  634. validate_insn_alternatives (d)
  635.      struct data *d;
  636. {
  637.   register int n = 0, start;
  638.   /* Make sure all the operands have the same number of
  639.      alternatives in their constraints.
  640.      Let N be that number.  */
  641.   for (start = 0; start < d->n_operands; start++)
  642.     if (d->op_n_alternatives[start] > 0)
  643.       {
  644.     if (n == 0)
  645.       n = d->op_n_alternatives[start];
  646. #ifdef APPLE_HAX
  647.     else if (n != d->op_n_alternatives[start]) {
  648.       error ("wrong number of alternatives in operand %d of insn number %d",
  649.          start, d->index_number);
  650.       fprintf(stderr, "File Pos %ld\n", ftell(infile));
  651.     }
  652.       }
  653. #else
  654.     else if (n != d->op_n_alternatives[start])
  655.       error ("wrong number of alternatives in operand %d of insn number %d",
  656.          start, d->index_number);
  657.       }
  658. #endif
  659.   /* Record the insn's overall number of alternatives.  */
  660.   d->n_alternatives = n;
  661. }
  662.  
  663. /* Look at a define_insn just read.  Assign its code number.
  664.    Record on insn_data the template and the number of arguments.
  665.    If the insn has a hairy output action, output a function for now.  */
  666.  
  667. static void
  668. gen_insn (insn)
  669.      rtx insn;
  670. {
  671.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  672.   register int i;
  673.  
  674.   d->code_number = next_code_number++;
  675.   d->index_number = next_index_number;
  676.   if (XSTR (insn, 0)[0])
  677.     d->name = XSTR (insn, 0);
  678.   else
  679.     d->name = 0;
  680.  
  681.   /* Build up the list in the same order as the insns are seen
  682.      in the machine description.  */
  683.   d->next = 0;
  684.   if (end_of_insn_data)
  685.     end_of_insn_data->next = d;
  686.   else
  687.     insn_data = d;
  688.  
  689.   end_of_insn_data = d;
  690.  
  691.   max_opno = -1;
  692.   num_dups = 0;
  693.  
  694.   mybzero (constraints, sizeof constraints);
  695.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  696.   mybzero (predicates, sizeof predicates);
  697.   mybzero (address_p, sizeof address_p);
  698.   mybzero (modes, sizeof modes);
  699.   mybzero (strict_low, sizeof strict_low);
  700.   mybzero (seen, sizeof seen);
  701.  
  702.   for (i = 0; i < XVECLEN (insn, 1); i++)
  703.     scan_operands (XVECEXP (insn, 1, i), 0, 0);
  704.  
  705.   d->n_operands = max_opno + 1;
  706.   d->n_dups = num_dups;
  707.  
  708.   mybcopy (constraints, d->constraints, sizeof constraints);
  709.   mybcopy (op_n_alternatives, d->op_n_alternatives, sizeof op_n_alternatives);
  710.   mybcopy (predicates, d->predicates, sizeof predicates);
  711.   mybcopy (address_p, d->address_p, sizeof address_p);
  712.   mybcopy (modes, d->modes, sizeof modes);
  713.   mybcopy (strict_low, d->strict_low, sizeof strict_low);
  714.  
  715.   validate_insn_alternatives (d);
  716.   process_template (d, XSTR (insn, 3));
  717. }
  718.  
  719. /* Look at a define_peephole just read.  Assign its code number.
  720.    Record on insn_data the template and the number of arguments.
  721.    If the insn has a hairy output action, output it now.  */
  722.  
  723. static void
  724. gen_peephole (peep)
  725.      rtx peep;
  726. {
  727.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  728.   register int i;
  729.  
  730.   d->code_number = next_code_number++;
  731.   d->index_number = next_index_number;
  732.   d->name = 0;
  733.  
  734.   /* Build up the list in the same order as the insns are seen
  735.      in the machine description.  */
  736.   d->next = 0;
  737.   if (end_of_insn_data)
  738.     end_of_insn_data->next = d;
  739.   else
  740.     insn_data = d;
  741.  
  742.   end_of_insn_data = d;
  743.  
  744.   max_opno = -1;
  745.   mybzero (constraints, sizeof constraints);
  746.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  747.   mybzero (predicates, sizeof predicates);
  748.   mybzero (address_p, sizeof address_p);
  749.   mybzero (modes, sizeof modes);
  750.   mybzero (strict_low, sizeof strict_low);
  751.   mybzero (seen, sizeof seen);
  752.  
  753.   /* Get the number of operands by scanning all the
  754.      patterns of the peephole optimizer.
  755.      But ignore all the rest of the information thus obtained.  */
  756.   for (i = 0; i < XVECLEN (peep, 0); i++)
  757.     scan_operands (XVECEXP (peep, 0, i), 0, 0);
  758.  
  759.   d->n_operands = max_opno + 1;
  760.   d->n_dups = 0;
  761.  
  762.   mybcopy (constraints, d->constraints, sizeof constraints);
  763.   mybcopy (op_n_alternatives, d->op_n_alternatives, sizeof op_n_alternatives);
  764.   mybzero (d->predicates, sizeof predicates);
  765.   mybzero (d->address_p, sizeof address_p);
  766.   mybzero (d->modes, sizeof modes);
  767.   mybzero (d->strict_low, sizeof strict_low);
  768.  
  769.   validate_insn_alternatives (d);
  770.   process_template (d, XSTR (peep, 2));
  771. }
  772.  
  773. /* Process a define_expand just read.  Assign its code number,
  774.    only for the purposes of `insn_gen_function'.  */
  775.  
  776. static void
  777. gen_expand (insn)
  778.      rtx insn;
  779. {
  780.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  781.   register int i;
  782.  
  783.   d->code_number = next_code_number++;
  784.   d->index_number = next_index_number;
  785.   if (XSTR (insn, 0)[0])
  786.     d->name = XSTR (insn, 0);
  787.   else
  788.     d->name = 0;
  789.  
  790.   /* Build up the list in the same order as the insns are seen
  791.      in the machine description.  */
  792.   d->next = 0;
  793.   if (end_of_insn_data)
  794.     end_of_insn_data->next = d;
  795.   else
  796.     insn_data = d;
  797.  
  798.   end_of_insn_data = d;
  799.  
  800.   max_opno = -1;
  801.   num_dups = 0;
  802.  
  803.   /* Scan the operands to get the specified predicates and modes,
  804.      since expand_binop needs to know them.  */
  805.  
  806.   mybzero (constraints, sizeof constraints);
  807.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  808.   mybzero (predicates, sizeof predicates);
  809.   mybzero (address_p, sizeof address_p);
  810.   mybzero (modes, sizeof modes);
  811.   mybzero (strict_low, sizeof strict_low);
  812.   mybzero (seen, sizeof seen);
  813.  
  814.   if (XVEC (insn, 1))
  815.     for (i = 0; i < XVECLEN (insn, 1); i++)
  816.       scan_operands (XVECEXP (insn, 1, i), 0, 0);
  817.  
  818.   d->n_operands = max_opno + 1;
  819.   d->n_dups = num_dups;
  820.  
  821.   mybcopy (constraints, d->constraints, sizeof constraints);
  822.   mybcopy (op_n_alternatives, d->op_n_alternatives, sizeof op_n_alternatives);
  823.   mybcopy (predicates, d->predicates, sizeof predicates);
  824.   mybcopy (address_p, d->address_p, sizeof address_p);
  825.   mybcopy (modes, d->modes, sizeof modes);
  826.   mybcopy (strict_low, d->strict_low, sizeof strict_low);
  827.  
  828.   d->template = 0;
  829.   d->outfun = 0;
  830.   validate_insn_alternatives (d);
  831. }
  832.  
  833. /* Process a define_split just read.  Assign its code number,
  834.    only for reasons of consistency and to simplify genrecog.  */
  835.  
  836.  
  837. static void
  838. gen_split (split)
  839.      rtx split;
  840. {
  841.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  842.   register int i;
  843.  
  844.   d->code_number = next_code_number++;
  845.   d->index_number = next_index_number;
  846.   d->name = 0;
  847.  
  848.   /* Build up the list in the same order as the insns are seen
  849.      in the machine description.  */
  850.   d->next = 0;
  851.   if (end_of_insn_data)
  852.     end_of_insn_data->next = d;
  853.   else
  854.     insn_data = d;
  855.  
  856.   end_of_insn_data = d;
  857.  
  858.   max_opno = -1;
  859.   num_dups = 0;
  860.  
  861.   mybzero (constraints, sizeof constraints);
  862.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  863.   mybzero (predicates, sizeof predicates);
  864.   mybzero (address_p, sizeof address_p);
  865.   mybzero (modes, sizeof modes);
  866.   mybzero (strict_low, sizeof strict_low);
  867.   mybzero (seen, sizeof seen);
  868.  
  869.   /* Get the number of operands by scanning all the
  870.      patterns of the split patterns.
  871.      But ignore all the rest of the information thus obtained.  */
  872.   for (i = 0; i < XVECLEN (split, 0); i++)
  873.     scan_operands (XVECEXP (split, 0, i), 0, 0);
  874.  
  875.   d->n_operands = max_opno + 1;
  876.  
  877.   mybzero (d->constraints, sizeof constraints);
  878.   mybzero (d->op_n_alternatives, sizeof op_n_alternatives);
  879.   mybzero (d->predicates, sizeof predicates);
  880.   mybzero (d->address_p, sizeof address_p);
  881.   mybzero (d->modes, sizeof modes);
  882.   mybzero (d->strict_low, sizeof strict_low);
  883.  
  884.   d->n_dups = 0;
  885.   d->n_alternatives = 0;
  886.   d->template = 0;
  887.   d->outfun = 0;
  888. }
  889.  
  890. char *
  891. xmalloc (size)
  892.      unsigned size;
  893. {
  894.   register char *val = (char *) malloc (size);
  895.  
  896.   if (val == 0)
  897.     fatal ("virtual memory exhausted");
  898.   return val;
  899. }
  900.  
  901. char *
  902. xrealloc (ptr, size)
  903.      char *ptr;
  904.      unsigned size;
  905. {
  906.   char *result = (char *) realloc (ptr, size);
  907.   if (!result)
  908.     fatal ("virtual memory exhausted");
  909.   return result;
  910. }
  911.  
  912. static void
  913. mybzero (b, length)
  914.      register char *b;
  915.      register unsigned length;
  916. {
  917.   while (length-- > 0)
  918.     *b++ = 0;
  919. }
  920.  
  921. static void
  922. mybcopy (b1, b2, length)
  923.      register char *b1;
  924.      register char *b2;
  925.      register unsigned length;
  926. {
  927.   while (length-- > 0)
  928.     *b2++ = *b1++;
  929. }
  930.  
  931. static void
  932. fatal (s, a1, a2, a3, a4)
  933.      char *s;
  934. {
  935.   fprintf (stderr, "genoutput: ");
  936.   fprintf (stderr, s, a1, a2, a3, a4);
  937.   fprintf (stderr, "\n");
  938.   exit (FATAL_EXIT_CODE);
  939. }
  940.  
  941. /* More 'friendly' abort that prints the line and file.
  942.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  943.  
  944. void
  945. fancy_abort ()
  946. {
  947.   fatal ("Internal gcc abort.");
  948. }
  949.  
  950. static void
  951. error (s, a1, a2)
  952.      char *s;
  953. {
  954.   fprintf (stderr, "genoutput: ");
  955.   fprintf (stderr, s, a1, a2);
  956.   fprintf (stderr, "\n");
  957. }
  958.  
  959. int
  960. main (argc, argv)
  961.      int argc;
  962.      char **argv;
  963. {
  964.   rtx desc;
  965. #ifndef APPLE_HAX
  966.     FILE *infile;
  967. #endif
  968.   register int c;
  969.  
  970.   obstack_init (rtl_obstack);
  971.  
  972.   if (argc <= 1)
  973.     fatal ("No input file name.");
  974.  
  975.   infile = fopen (argv[1], "r");
  976.   if (infile == 0)
  977.     {
  978.       perror (argv[1]);
  979.       exit (FATAL_EXIT_CODE);
  980.     }
  981.  
  982.   init_rtl ();
  983.  
  984.   output_prologue ();
  985.   next_code_number = 0;
  986.   next_index_number = 0;
  987.   have_constraints = 0;
  988.  
  989.   /* Read the machine description.  */
  990.  
  991.   while (1)
  992.     {
  993.       c = read_skip_spaces (infile);
  994.       if (c == EOF)
  995.     break;
  996.       ungetc (c, infile);
  997.  
  998.       desc = read_rtx (infile);
  999.       if (GET_CODE (desc) == DEFINE_INSN)
  1000.     gen_insn (desc);
  1001.       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  1002.     gen_peephole (desc);
  1003.       if (GET_CODE (desc) == DEFINE_EXPAND)
  1004.     gen_expand (desc);
  1005.       if (GET_CODE (desc) == DEFINE_SPLIT)
  1006.     gen_split (desc);
  1007.       next_index_number++;
  1008.     }
  1009.  
  1010.   output_epilogue ();
  1011.  
  1012.   fflush (stdout);
  1013.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  1014.   /* NOTREACHED */
  1015.   return 0;
  1016. }
  1017.  
  1018. static int
  1019. n_occurrences (c, s)
  1020.      char c;
  1021.      char *s;
  1022. {
  1023.   int n = 0;
  1024.   while (*s)
  1025.     n += (*s++ == c);
  1026.   return n;
  1027. }
  1028.